package cn.skidz.im.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import java.util.ArrayList;
import java.util.List;

import cn.skidz.im.R;
import cn.skidz.im.common.IntentExtra;
import cn.skidz.im.db.model.GroupExitedMemberInfo;
import cn.skidz.im.model.GroupMember;
import cn.skidz.im.model.Resource;
import cn.skidz.im.model.Status;
import cn.skidz.im.ui.adapter.GroupExitedListAdapter;
import cn.skidz.im.ui.adapter.GroupManagerAdapter;
import cn.skidz.im.ui.adapter.GroupMuteListAdapter;
import cn.skidz.im.ui.dialog.CommonDialog;
import cn.skidz.im.utils.ToastUtils;
import cn.skidz.im.viewmodel.GroupExitedInfoViewModel;
import cn.skidz.im.viewmodel.GroupManagementViewModel;

public class GroupMuteListActivity extends TitleBaseActivity {

    private ListView groupMuteList;
    private GroupMuteListAdapter groupMuteManagerAdapter;
    private GroupManagementViewModel groupManagementViewModel;
    private String groupId;
    private List<GroupMember> memberList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_mute_manager);
        groupId = getIntent().getStringExtra(IntentExtra.GROUP_ID);
        initView();
        initViewModel();
    }

    private void initView() {
        getTitleBar().setTitle(getString(R.string.seal_group_manager_mute_title));
        groupMuteList = findViewById(R.id.lv_group_mute_list);
        groupMuteManagerAdapter = new GroupMuteListAdapter();
        groupMuteList.setAdapter(groupMuteManagerAdapter);
        groupMuteManagerAdapter.setOnGroupManagerListener(new GroupMuteListAdapter.OnGroupMuteListener() {
            @Override
            public void onSelected(int position, boolean selected) {
                showConfirmDialog(position, selected);
            }
        });
    }

    private void initViewModel() {
        groupManagementViewModel = ViewModelProviders.of(this, new GroupManagementViewModel.Factory(groupId, getApplication())).get(GroupManagementViewModel.class);
        // 除群主之外的群成员
        groupManagementViewModel.getGroupMembersWithoutGroupOwner().observe(this, new Observer<List<GroupMember>>() {
            @Override
            public void onChanged(List<GroupMember> members) {
                if(members!=null&&members.size()>0){
                    memberList = members;
                    groupMuteManagerAdapter.updateList(memberList);
                }
            }
        });

        groupManagementViewModel.getMuteMemberResult().observe(this, new Observer<Resource<Void>>() {
            @Override
            public void onChanged(Resource<Void> voidResource) {
                if (voidResource.status == Status.SUCCESS) {
                    ToastUtils.showToast(R.string.seal_group_manager_set_success);
                } else if (voidResource.status == Status.ERROR) {
                    ToastUtils.showToast(R.string.seal_group_manager_set_fail);
                }
            }
        });
    }

    /**
     * 显示成员禁言的 dialog
     *
     * @param position
     */
    private void showConfirmDialog(int position, boolean selected) {
        CommonDialog.Builder builder = new CommonDialog.Builder();
        GroupMember groupMember = memberList.get(position);
        builder.setContentMessage(getString(selected ? R.string.seal_group_manager_mute_dialog_content : R.string.seal_group_manager_remute_dialog_content, groupMember.getName()));
        builder.setDialogButtonClickListener(new CommonDialog.OnDialogButtonClickListener() {
            @Override
            public void onPositiveClick(View v, Bundle bundle) {
                muteGroupMember(groupMember.getUserId(), selected);
            }

            @Override
            public void onNegativeClick(View v, Bundle bundle) {

            }
        });
        CommonDialog dialog = builder.build();
        dialog.show(getSupportFragmentManager(), "transfer_dialog");

    }

    /**
     * 群成员禁言
     *
     * @param userId
     */
    private void muteGroupMember(String userId, boolean selected) {
        if (groupManagementViewModel != null) {
            if (selected) {
                groupManagementViewModel.setMuteMember(userId,1);
            } else {
                groupManagementViewModel.setMuteMember(userId,0);
            }
        }
    }

}
